import Common from '../../common/common.js';
let common = new Common;
let timer;
Page({
  data: {
    userName: '', //姓名
    imgUrls: '',
    code: 0, //验证码状态控制
    authCode: '',
    phone: '',
    miaoNum: 60,
    isRegister: false,
    registerBand: false, //注册按钮屏蔽
    loginBand: false, //登陆按钮屏蔽
  },
  detile:function(){
    wx.navigateTo({
      url: '/pages/myCenter/problemDetil/problemDetil?code='+1+'&type='+1,
    })
  },
  onLoad(options) {
    this.setData({
      headPicUrl: options.headPicUrl || '',
      sex: options.gender || 1
    })
    this.setData({
      isRegister: getApp().globalData.yaoQorgSeq ? true : false
    })
    this.getList()
  },
  getUserName(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  registerClick() {
    if (!this.data.userName) {
      wx.showToast({
        title: '请填写您的姓名！',
        icon: 'none'
      })
      return false;
    }
    if (!this.data.phone) {
      wx.showToast({
        title: '请填写您的手机号码！',
        icon: 'none'
      })
      return false;
    }
    if (!this.data.authCode) {
      wx.showToast({
        title: '请先获取验证码！',
        icon: 'none'
      })
      return false;
    }
    let data = {
      realName: this.data.userName,
      account: this.data.phone,
      authCode: this.data.authCode,
      orgSeq: getApp().globalData.yaoQorgSeq,
      headPicUrl: this.data.headPicUrl,
      sex: this.data.gender
    }
    this.setData({
      registerBand: true
    })
    common.requestAjax('/api/user-service/v1/authMa/register', 'POST', data).then(res => {
      if (res.data.code == 1) {
        wx.showToast({
          title: '注册成功',
        })
        // 登录成功存储信息
        wx.setStorageSync('token', res.data.result.token);
        wx.setStorage({
          data: true,
          key: 'isLogin',
        })
        getApp().globalData.orgSe = res.data.result.userId;
        getApp().globalData.orgSeq = res.data.result.orgList[0].orgSeq;
        getApp().globalData.userType = res.data.result.orgList[0].userType;
        getApp().globalData.orgList = res.data.result.orgList; //店铺列表
        //成功后删除
        getApp().globalData.yaoQorgSeq = "";
        wx.reLaunch({
          url: '/pages/index/index',
        })
      } else {
        if (res.data.message.indexOf("已注册") > -1) {
          wx.showModal({
            title: '提示',
            content: '您的手机号已注册,请前往登录',
            showCancel: false,
            confirmText: '去登录',
            success: res => {
              if (res.confirm) {
                this.setData({
                  isRegister: false,
                  code: false,
                  miaoNum: 60,
                  authCode: ''
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: "none"
          })
        }
        this.setData({
          registerBand: false
        })
      }
    }).catch(err => {
      console.log(err);
      this.setData({
        registerBand: false
      })
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      });
    })
  },
  login: function (e) {
    let data = {
      account: this.data.phone,
      authCode: this.data.authCode
    }

    if (data.account.length !== 11) {
      wx.showToast({
        title: '手机号格式有误！',
        icon: 'none'
      })
      return
    }

    //验证码正则 
    if (data.authCode.length !== 6) {
      wx.showToast({
        title: '验证码有误！',
        icon: 'none'
      })
      return
    }
    this.setData({
      loginBand: true
    })
    common.requestAjax('/api/user-service/v1/authMa/loginByPhone', 'POST', data).then(res => {
      if (res.data.code == 1) {
        wx.showToast({
          title: '登录成功',
        })
        // 登录成功存储信息
        wx.setStorageSync('token', res.data.result.token);
        wx.setStorage({
          data: true,
          key: 'isLogin',
        })
        getApp().globalData.orgSe = res.data.result.userId;
        getApp().globalData.orgSeq = res.data.result.orgList[0].orgSeq;
        getApp().globalData.userType = res.data.result.orgList[0].userType;
        getApp().globalData.orgList = res.data.result.orgList; //店铺列表
        // let routeUrl = '/pages/index/index';
        // let routeType = 'navigateTo';
        // let params = '';
        // common.routeLink(routeUrl, params, routeType)
        wx.redirectTo({
          url: '/pages/index/index',
        })
      } else {
        this.setData({
          loginBand: false
        })
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
      }
    }).catch(err => {
      console.log(err);
      this.setData({
        loginBand: false
      })
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      });
    })
  },
  getLoginCode: function () {
    if (!this.data.phone) {
      wx.showToast({
        title: '手机号码不能为空',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return;
    }

    this.getSenMsg();
  },
  phone: function (e) {
    this.setData({
      phone: e.detail.value.replace(/\s+/g, '')
    })
  },
  getCode: function (e) {
    this.setData({
      authCode: e.detail.value
    })
  },
  getSenMsg() {
    let data = {
      phone: this.data.phone,
      code: '1011012092218',
      msgType: 1
    }
    common.requestAjax('/api/user-service/v1/foreign/getUmsDto', 'POST', data).then(res => {
      if (res.data.code == 1) {
        this.setData({
          code: 1
        })
        let miaoNum = this.data.miaoNum;
        timer = setInterval(() => {
          if (miaoNum < 2) {
            clearInterval(timer);
            this.setData({
              code: 0,
              miaoNum: 60,
            })
          } else {
            miaoNum = miaoNum - 1
            this.setData({
              miaoNum
            })
          }
        }, 1000)
        common.showToast(res.data.message)
      } else {
        wx.showToast({
          title: res.data.message,
          icon: "none"
        })
      }
    }).catch(err => {
      console.log(err);
    })
  },
  getList: function (e) {
    common.requestAjax('/api/promotion-service/v1/PageConfig/queryShopConfigurationInfor', 'GET').then(res => {
      if (res.data.code == 1) {
        this.setData({
          imgUrls: res.data.result.picUrl
        })

      }
    }).catch(err => {
      console.log(err);
    })
  },
})